package fr.esipe.eioc3.timetableontime_v2;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import fr.esipe.agenda.parser.Event;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TimeTableListView extends BaseExpandableListAdapter{

	private Context context;
	List<Event> parsedEventList;
	private ArrayList<GroupDays> groupes;
	private LayoutInflater mInflater;
	List<String> jours;

	public TimeTableListView(Context context, ArrayList<GroupDays> groupes ) {
		//this.parsedEventList = parsedEventList;
		this.context = context;
		this.groupes = groupes;
		mInflater = LayoutInflater.from(context);
		//this.jours = jours;

	}

	@Override
	    public boolean areAllItemsEnabled() {
	        return true;
	    }
	
	@Override
	public ChildLabels getChild(int gPosition, int cPosition) {
		return groupes.get(gPosition).getObjets().get(cPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		final ChildLabels child = (ChildLabels) getChild(groupPosition, childPosition);

		ChildViewHolder childViewHolder;
		//startEvent.setTime(parsedEventList.get(childPosition).getStartTime());

		if (convertView == null) {
			childViewHolder = new ChildViewHolder();
			
			convertView = mInflater.inflate(R.layout.group_child, null);
			
			childViewHolder.textViewChild = (TextView) convertView.findViewById(R.id.cours);
			childViewHolder.textViewChild.setSoundEffectsEnabled(true);
			childViewHolder.textViewChild.setTextSize(18);
			childViewHolder.textViewChild.setTextColor(Color.WHITE);
			childViewHolder.textViewChild.setBackgroundColor(Color.GRAY);			
			childViewHolder.textViewChild.setText(child.toString());
			
			convertView.setTag(childViewHolder);
			
			Log.d("debug", "dans le childview, chilpview position = " + childPosition);
			
		} 
		else {
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}	
		
		Log.d("debug", "on est ici pour voir");
		
		
		childViewHolder.textViewChild.setText(child.toString());
		
		childViewHolder.textViewChild.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(context, "Infos :\n" + child.getLabels(), Toast.LENGTH_LONG).show();
			}
		});
		return convertView;
	}

	@Override
	public int getChildrenCount(int gPosition) {
		return groupes.get(gPosition).getObjets().size();
	}

	@Override
	public Object getGroup(int gPosition) {
		return groupes.get(gPosition);
	}

	@Override
	public int getGroupCount() {
		return groupes.size();
	}

	@Override
	public long getGroupId(int gPosition) {
		return gPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupViewHolder gholder;
		
		GroupDays group = (GroupDays) getGroup(groupPosition);
		if (convertView == null) {
			gholder = new GroupViewHolder();
			convertView = mInflater.inflate(R.layout.group_row, null);
			gholder.textViewGroup = (TextView) convertView.findViewById(R.id.TVGroup);
			gholder.textViewGroup.setTextColor(Color.WHITE);
			gholder.textViewGroup.setBackgroundColor(Color.BLACK);
			convertView.setTag(gholder);
		} else {
			gholder = (GroupViewHolder) convertView.getTag();
		}
		
		gholder.textViewGroup.setText(group.getNom());
		return convertView;
	}
	
	@Override
	public boolean hasStableIds() {
		return true;
	}
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	class GroupViewHolder {
		public TextView textViewGroup;
	}
	class ChildViewHolder {
		public TextView textViewChild;
		public Button buttonChild;
		public void setText(String string) {
			// TODO Auto-generated method stub
			
		}
	}

}
